package com.lzy.web.facade.mq;

import com.lzy.framework.core.ensure.Ensure;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ellengou
 * Date: 2017-12-13
 * Time: PM 1:59
 */
@Service
public class RabbitmqTicketConsumerConfirmListener implements ChannelAwareMessageListener {

    private static Logger logger = LoggerFactory.getLogger(RabbitmqTicketConsumerConfirmListener.class);

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        logger.info("consumer--:" + message.getMessageProperties() + ":" + new String(message.getBody()));
        try {
            //TODO 业务处理
            String routingKey = message.getMessageProperties().getReceivedRoutingKey();
            String consumerQueue = message.getMessageProperties().getConsumerQueue();
            Ensure.that(consumerQueue).isNotNull("");
            if (consumerQueue.equalsIgnoreCase("QUEUE_TRAIN_CHANGEORDER_PAYTIME")) {
                System.out.println("QUEUE_TRAIN_CHANGEORDER_PAYTIME");
                System.out.println("处理时间 :  " + System.nanoTime());
            }

            if (consumerQueue.equalsIgnoreCase("TICKET_TEST_QUEUE_2")) {
                System.out.println("TICKET_TEST_QUEUE_2");
                System.out.println("处理时间 :  " + System.nanoTime());
            }

            //处理完成发送回执
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            //如果业务没有正常完成，此处需要异常回执
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }
}